﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using CoreData;
using SchoolManagementSystem.Details;
using DevExpress.XtraEditors.Controls;
using Helper;

namespace SchoolManagementSystem.DetailsManager
{
    public partial class UserUC:DetailUC
    {
        public UserUC()
        {
            InitializeComponent();
            this.ObjectName = "người dùng";
            this.Code = "User";
            this.Type = new Users();
            comboboxUCNhanvien.Model = RegisterService.RegisterServices.StaffService;
            comboboxUCNhanvien.Uc = new StaffUC();
            comboboxUCNhanvien.ColumnMain = "Name";
            comboboxUCNhanvien.ColumnDisplay = "Code,Name";
            masterMainUC1.Model = RegisterService.RegisterServices.User_Module_RoleService;
            masterMainUC1.DetailUCControl = new GrandPermissionUserUC();
            LoadUserGroup();
          //  com
        }

        private void LoadUserGroup()
        {
            try
            {
                var userGroups = RegisterService.RegisterServices.GroupService.GetListByLINQ(c => c.Active == true);
                ComboBoxItemCollection coll = cbgroup.Properties.Items;
                coll.BeginUpdate();
                try
                {
                    cbgroup.Properties.NullText = "[Rỗng]";
                    cbgroup.Properties.Items.AddRange(userGroups.ToList());
                }
                finally
                {
                    coll.EndUpdate();
                }
                cbgroup.SelectedIndex = 0;


            }
            catch { }
        }

        String UserID="1=1";
        protected override long DetailDisplay()
        {
            Users user = new Users();
            Group group = new Group();
            if (ID > 0)
            {
                user = (Users)RegisterService.RegisterServices.UserService.GetByID(ID);
                UserID = ID.ToString();//user.UserGuidID;
                long groupid = RegisterService.RegisterServices.User_GroupService.GetOneByLINQ(c => c.UserID == ID).GroupID;
                cbgroup.EditValue = (Group)RegisterService.RegisterServices.GroupService.GetByID(groupid);
            }
            txtpassword.Text = user.PassWord;
            txtaccount.Text = user.UserName;
          //  Staff staff = RegisterService.RegisterServices.StaffService.GetByGuidID(user.StaffGuidID);

            comboboxUCNhanvien.ID = user.StaffID;
            dateEdit_from.EditValue = user.StartDate;
            dateEdit_to.EditValue = user.Endate;
            ckactive.Checked = user.Lock;
            masterMainUC1.SearchString = " UserID='"+UserID+"'"; //get danh sach phan quyen
            masterMainUC1.DetailUCControl.SearchString = " UserID='" + UserID + "'";
            return ID;

        }

        protected override long Save()
        {
            Users user = new Users();
            if (ID > 0)
            {
                user = (Users)RegisterService.RegisterServices.UserService.GetByID(ID);
            }
            user.UserName = txtaccount.Text;
            user.PassWord = txtpassword.Text;
            user.StartDate = (DateTime)dateEdit_from.EditValue;
            user.Endate = (DateTime)dateEdit_to.EditValue;
            user.StaffID = comboboxUCNhanvien.ID; //((Staff)RegisterService.RegisterServices.StaffService.GetByID(comboboxUCNhanvien.ID)).StaffGuidID;
            user.Lock = ckactive.Checked;
            ID = RegisterService.RegisterServices.UserService.Save(user);
            var group = cbgroup.SelectedItem as Group;
            if (ID > 0)
            {
                User_Group usergroup = RegisterService.RegisterServices.User_GroupService.GetOneByLINQ(c => c.UserID == ID && c.GroupID == group.ID);
                if (usergroup == null)
                {
                    usergroup = new User_Group();
                    usergroup.GroupID = group.ID;
                    usergroup.UserID = ID;
                }
                RegisterService.RegisterServices.User_GroupService.Save(usergroup);
            }
            masterMainUC1.DetailUCControl.SearchString = ID.ToString();//((Users)RegisterService.RegisterServices.UserService.GetByID(ID)).UserGuidID;
            return ID;
        }


        public override bool Delete(long SelectedID)
        {
            return RegisterService.RegisterServices.UserService.Delete((Users)RegisterService.RegisterServices.UserService.GetByID(SelectedID));

        }

        private void UserUC_Load(object sender, EventArgs e)
        {
            AddTextChanged(xtraTabPage1.Controls);
            AddTextChanged(xtraTabPage2.Controls);
        }


     
    }
}
